package com.sudo.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.DateUtil;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.mapper.my.SysUserMapper;
import com.sudo.mapper.my.UserFileMapper;
import com.sudo.pojo.SysUser;
import com.sudo.pojo.UserFile;
import com.sudo.service.BaseService;
import com.sudo.service.UserFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.concurrent.Executors;

/**
 * @author：为道日损 2020-06-22 10:01
 * webSite：https://www.sudo.ren
 */
@Service
public class UserFileServiceImpl extends BaseService implements UserFileService {

    @Autowired
    private UserFileMapper userFileMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int addUserFile(String username ,String oldFileName,String newFileName,String filePosPath) {
        UserFile userFile = new UserFile();
        userFile.setUsername(username);
        userFile.setOldName(oldFileName);
        userFile.setFileName(newFileName);
        userFile.setFilePath(filePosPath);
        String dateTime = DateUtil.dateToStringWithTime();
        userFile.setUploadTime(dateTime);
        return userFileMapper.insertSelective(userFile);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public PagedGridResult getAllFilesByPage(Integer page , Integer pageSize,
                                             String username,String startDate,String endDate) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(UserFile.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(username)) {
            criteria.andEqualTo("username", username);
        }
        if (StringUtils.isNotBlank(startDate)) {
            criteria.andGreaterThanOrEqualTo("uploadTime", startDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("uploadTime", endDate);
        }
        List<UserFile> sysUserList = userFileMapper.selectByExample(example);

        return setterPagedGrid(sysUserList ,page);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int updateUserLogo(String sessionUser, String filePosPath) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", sessionUser);

        SysUser sysUser = new SysUser();
        sysUser.setProfilePhoto(filePosPath);
        sysUser.setUpdateBy(sessionUser);
        String dateTime = DateUtil.dateToStringWithTime();
        sysUser.setUpdateTime(dateTime);
        return sysUserMapper.updateByExampleSelective(sysUser,example);
    }
}
